package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.junit.Test;
import org.omg.CORBA.PUBLIC_MEMBER;

public class ReflectTest {
	/**
	 * 
	 * @param args
	 * @throws Exception 
	 */
public static void main(String[] args) throws Exception {
	//方法一 加载类的字节码
	//Class<?> clazz = Class.forName("Person");常用
	//方法二
/*	Class clazz2= Person.class;
	
	//方式三
	Class clazz3 = new Person().getClass();
	*/
	
	
	//利用constructor创建对象
/*	Class clazz2 = Class.forName("reflect.Person");
	Constructor constructor=clazz2.getConstructor(null);
Object object= constructor.newInstance(null);//实例
System.out.println(object);*/
	
	
}
@Test
public void test2() throws Exception{
	Class class1=Class.forName("reflect.Person");
	Constructor constructor= class1.getConstructor(String.class);
	Object obj=constructor.newInstance("xiyaq");
	System.out.println(obj);
	
}

@Test
public void test3() throws Exception, Exception, IllegalArgumentException, InvocationTargetException{
	Class class3=Class.forName("reflect.Person");
	Constructor constructor3= class3.getConstructor(int.class);
	constructor3.setAccessible(true);//暴力反射
	Person person= (Person) constructor3.newInstance(1);
	System.out.println(person);
}
}
